/*
 * Daniel R Padilla
 *
 * Copyright (c) 2008, Daniel R Padilla
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.databasefacade.select;


import org.databasefacade.table.Table;
import org.databasefacade.table.column.Column;


/**
 * All the operators in this class have
 * ansiSQL standard definitions so the
 * class is not declared as abstract
 * because ansiSQL-92 is sufficient.
 */
public class Operator {	

    /** Arithmetic Operators */
    public static final int ADD = 1;
    public static final int SUBTRACT = 2;
    public static final int MULTIPLY = 3;
    public static final int DIVIDE = 4;
    public static final int MODULO = 5;
	
    /** Comparison Operators */
    public static final int EQUAL = 10;
    public static final int NOT_EQUAL = 11;
    public static final int LESS_THAN = 12;
    public static final int LESS_THAN_EQUAL_TO = 13;
    public static final int GREATER_THAN = 14;
    public static final int GREATER_THAN_EQUAL_TO = 15;
	
    protected static String assign(
            Table table1, Column field1, 
            Table table2, Column field2,
            int operatorType) {
        return "(" + table1.getTableAlias() + "." + field1.getColumnName() + operator(operatorType) + table2.getTableAlias() + "."
                + field2.getColumnName() + ")";
    }
    
    protected static String assign(
            Column field1, 
            Column field2,
            int operatorType) {
        return "(" + field1.getTableAlias() + "." + field1.getColumnName() + operator(operatorType) + field2.getTableAlias() + "."
                + field2.getColumnName() + ")";
    }

    protected static String assign(Table table1, Column field1, 
            Object value, int operatorType) {
    	String d = table1.getDBHelper().getTranslator().getStringDelimiter();
        return "(" + table1.getTableAlias() + "." + field1.getColumnName() + operator(operatorType) + d + value.toString() + d + ")";
    }
    
    protected static String assign(Column field, 
            Object value, int operatorType) {
    	String d = field.getStringDelimiter();
        return "(" + field.getTableAlias() + "." + field.getColumnName() + operator(operatorType) + d + value.toString() + d + ")";
    }

    protected static String assign(Table table, Column field, 
            Operator innerOperator, int operatorType) {
        return "(" + table.getTableAlias() + "." + field.getColumnName() + operator(operatorType) + innerOperator.toString()
                + ")";
    }
    
    protected static String assign(Column field, 
            Operator innerOperator, int operatorType) {
        return "(" + field.getTableAlias() + "." + field.getColumnName() + operator(operatorType) + innerOperator.toString()
                + ")";
    }

    public static String operator(int operatorType) {
        switch (operatorType) {
        case ADD:
            return "+";

        case SUBTRACT:
            return "-";

        case MULTIPLY:
            return "*";

        case DIVIDE:
            return "/";

        case MODULO:
            return "%";

        case EQUAL:
            return "=";

        case NOT_EQUAL:
            return "<>";

        case LESS_THAN:
            return "<";

        case LESS_THAN_EQUAL_TO:
            return "<=";

        case GREATER_THAN:
            return ">";

        case GREATER_THAN_EQUAL_TO:
            return ">=";
        }
        return null;
    }
}
